//
//  AY_BookCell3.m
//  AiYue
//
//  Created by boWen on 2023/10/17.
//

#import "AY_BookCell3.h"
#import "AY_BookTuijCell.h"
#import "AY_BookDetailViewController.h"
@interface AY_BookCell3 ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) UIView * bg_View;//底部圆角

/** 标题  */
@property (strong,nonatomic) UILabel * topTitle;
@property (nonatomic, strong) UITableView * baseTableV;
@property (nonatomic, strong) NSMutableArray * sourceAry;//数据源
@property (nonatomic, strong) UIButton * changeBtn;//换一换
@property (nonatomic, strong) UIImageView * changeImage;//换一换
@property (strong,nonatomic) UILabel * subTitle;

@end


@implementation AY_BookCell3


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = COLOR_Bg;
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
    self.topTitle = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(15));
        make.left.equalTo(self.bg_View).offset(YNUM(15));
    }];
    
    self.changeImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_change") andBg:COLOR_Bg andRadius:YNUM(4) andAddSubView:self.bg_View];
    self.changeImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setChangeAction3)];
    [self.changeImage addGestureRecognizer:tap];
    [self.changeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(23.5));
        make.right.equalTo(self.bg_View).offset(YNUM(-12));
        make.height.mas_equalTo(YNUM(13));
        make.width.mas_equalTo(YNUM(13));
    }];
    
    self.changeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"换一换" andFont:YFont(14) andTitleColor:COLOR(@"#999999") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setChangeAction3) andRadius:0 andAddSubView:self.bg_View];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(18));
        make.right.equalTo(self.changeImage.mas_left).offset(YNUM(-3));
        make.height.mas_equalTo(YNUM(26));
//        make.width.mas_equalTo(YNUM(100));
    }];
    
    self.subTitle = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#909090") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.subTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topTitle.mas_bottom);
        make.left.equalTo(self.topTitle.mas_left);
        make.height.mas_equalTo(YNUM(22));
    }];
   
    [self.bg_View addSubview:self.baseTableV];
    [self.baseTableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitle.mas_bottom).offset(YNUM(12));
        make.left.equalTo(self.topTitle.mas_left);
        make.right.equalTo(self.contentView.mas_right).offset(YNUM(-12));
        make.bottom.equalTo(self.contentView).offset(YNUM(-18));
    }];
}


- (UITableView *)baseTableV {
    if (!_baseTableV) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectZero];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor whiteColor];
        myTableView.scrollEnabled = NO;
        myTableView.tableFooterView = [[UIView alloc] init];
        [myTableView registerClass:[AY_BookTuijCell class] forCellReuseIdentifier:@"AY_BookTuijCell"];
        _baseTableV = myTableView;
    }
    return _baseTableV;
}

- (NSMutableArray *)sourceAry {
    if(!_sourceAry) {
        _sourceAry = [@[] mutableCopy];
    }
    return _sourceAry;
}
#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookTuijCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookTuijCell"];
    if(!cell) {
        cell = [[AY_BookTuijCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AY_BookTuijCell"];
    }
    if (self.sourceAry.count > indexPath.section) {
        cell.subModel = self.sourceAry[indexPath.section];
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sourceAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(92);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 0 : YNUM(10);
   
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, GK_SCREEN_WIDTH, YNUM(10));
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    if(self.sourceAry.count > indexPath.section) {
        bookDetailVC.bookId = [self.sourceAry[indexPath.section] bookDecId];
    }
    [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
}

#pragma mark - 自定义事件

- (void)setChangeAction3 {
    [AYApiRequest getMallContentRefreshWithControlId:kStringUtils(_model.controlId) success:^(AY_BookRefreshModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            if (kArrayIsEmpty(pkg.data)) {
                [self.sourceAry removeAllObjects];
            }else {
                [self.sourceAry removeAllObjects];
                NSLog(@"-ssa--%@", pkg.data);
                self.sourceAry = [NSMutableArray arrayWithArray:pkg.data];
                [self.baseTableV reloadData];
            }
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}


- (void)setModel:(ChannelListModel *)model {
    _model = model;
    self.sourceAry = [NSMutableArray arrayWithArray:_model.items];
    self.topTitle.text = kStringUtils(_model.name);
    self.subTitle.text = kStringUtils(_model.bookDescription);
    if(kStringIsEmpty(_model.moreBtnName)){
        self.changeBtn.hidden = YES;
        self.changeImage.hidden = YES;
    }else {
        self.changeBtn.hidden = NO;
        self.changeImage.hidden = NO;
        [self.changeBtn setTitle:kStringUtils(_model.moreBtnName) forState:UIControlStateNormal];
    }
    [self.baseTableV reloadData];
}






- (void)setFrame:(CGRect)frame {
    frame.origin.x += 16;
    frame.size.width -= 2 * 16;
    [super setFrame:frame];
}

@end
